import config from "../../../config.js";
const base = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uploadList:[],
    content:'',
    contact:'',
  },

  contentInput:function(e){
    this.setData({
        content:e.detail.value
    })
  },

  contactInput:function(e){
    this.setData({
        contact:e.detail.value
    })
  },

  uploadImg:function(){
    var that = this;
    wx.chooseImage({
      count:4,
      success: function(res) {
        that.setData({
            uploadList:res.tempFilePaths
        })
      },
    })
  },

  submit:function(){
    console.log("提交反馈")
    var that = this;
    if(!that.data.content) {
        wx.showToast({
          title: '请输入反馈内容',
          icon:'none'
        })
        return false;
    }else{
        wx.showLoading({
            title:'请稍等'
        })
        wx.request({
            url:base.feedBack,
            header:{
                'content-type': 'application/x-www-form-urlencoded',
                'Cookie': wx.getStorageSync("session")
            },
            data:{
                content:that.data.content,
                tel:that.data.contact
            },
            success:function(res){
                console.log(res);
                if(res.data.status == 200){
                    if(that.data.uploadList.length==0){
                        wx.hideLoading();
                        wx.showToast({
                            title:'提交成功'
                        })
                    }else{
                        //提交图片
                        var len = that.data.uploadList.length,
                            i = 0;
                        function upload(){
                            console.log(that.data.uploadList[i]);
                            wx.uploadFile({
                                url:base.uploadPic,
                                filePath:that.data.uploadList[i],
                                name:'picA',
                                header: {
                                    "Cookie": wx.getStorageSync("session"),
                                    "content-type":"multipart/form-data"
                                },
                                success:function(res){
                                    var res = JSON.parse(res.data);
                                    if(res.status == 200){
                                        console.log("第"+i+"张照片上传成功");
                                        i++;
                                        console.log(i + "*****" + len);
                                        if(i<len){
                                            console.log("开始上传第"+i+"张")
                                            upload();
                                        }else{
                                            wx.hideLoading();
                                            wx.showToast({
                                              title: '提交成功',
                                              icon:'none'
                                            })
                                        }
                                    }else{
                                        wx.hideLoading();
                                        wx.showToast({
                                            title: '图片上传失败',
                                            icon:'none'
                                        })
                                    }
                                },
                            })
                        }
                        upload();
                    }
                }else{
                    wx.hideLoading();
                    wx.showToast({
                      title: '提交失败',
                      icon: 'none'
                    })
                }
            }
        })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})